

local ui = class.panel:builder 
{
    x = (1920-650)/2,
    y = 200,
    w = 650,
    h = 500,
    fdf = 'tooltip_backdrop1',
    default = true,
    show_flag = false,
    map_list = {},
    add_map = function(self,name,data)
        local index = #self.map_list
        local button = class.button:builder{
            parent = self,
            x = self.w,
            y = self.h/2 + index*40,
            w = 150,
            h = 40,
            name = name,
            index = index,
            class = '菜单按钮',
            back = {
                type = 'panel',
                fdf = 'tooltip_backdrop1',
                level = 1,
            },
            title = {
                type = 'text',
                text = name,
                align = 5,
                font_size = 12,
                level = 2,
            }
        }
        button.map_data = data
        table.insert(self.map_list,button)
    end,
    update = function(self,data)
        local player = ac.player.self
        local data = self.show_data or {}
        local max = #self.icon_list
        local index = (self.page - 1)*max
        self.max_page = math.max(math.ceil(#data/max),1)
        self.page_text:set_text(('%s/%s'):format(self.page,self.max_page))
        for a,button in ipairs(self.icon_list) do
            local skill = data[index + a]
            button.skill = skill
            if skill and skill~='~' then
                local art = skill.art or [[ReplaceableTextures\CommandButtons\BTNSelectHeroOn.blp]]
                if skill.cs_art and get_cs_art then
                    art = get_cs_art(skill)
                end
                button:set_normal_image(art)
                button.text:set_text(skill.title or skill.name)
                button:show()
            else
                button:hide()
            end
        end
    end,
    on_button_clicked = function(self,button)
        local player = ac.player.self
        if button==self.page_add then
            self.page = math.min(self.page + 1,self.max_page)
            self:update()
        elseif button==self.page_los then
            self.page = math.max(self.page - 1,1)
            self:update()
        elseif button==self.esc then
            self:hide()
        elseif button.class=='菜单按钮' then
            self.show_data = button.map_data
            if self.select_map then
                self.select_map.title:set_color(0xffffffff)
            end
            self.select_map = button
            self.select_map.title:set_color(0xff00ff00)
            self.page = 1
            self:update()
        elseif button.class=='操作按钮' then
            local data = button.skill
            if data then
                if data.skills then
                    self.show_data = data.skills
                    self.page = 1
                    self:update()
                else
                    local mian = self.select_map
                    if mian then
                        local name = '测试-' .. mian.name
                        ac.sync(name,data.name)
                    end
                end
            end
        end
    end,
    on_button_mouse_enter = function(self,button)
        local skill = button.skill
        local player = ac.player.self
        if skill and not skill.is_empty then
            local tip = skill.tip
            button.art = skill.art
            button:tooltip(skill.title or skill.name,tip)
        end
    end,
    open = function(self)
        if self.show_flag then
            self:hide()
        else
            self:show()
        end
    end,
}

local w,h = 64,64
local g1 = 57
local g2 = 36
ui.icon_list = {}
for b=1,4 do
    for a=1,5 do
        local button = ui:add_button([[ui\ui_b0.tga]],(a-1)*(w+g1) + 30,(b-1)*(h+g2) + 40,w,h,1)
        button.text = button:add_text('名字名字',-30,h,w+60,25,10,5)
        button.icon = button:add_panel('')
        button.class = '操作按钮'
        button.text:set_color(0xff00ff00)
        button:hide()
        table.insert(ui.icon_list,button)
    end
end

local w0,h0 = 21,26
local w1 = 80
local x0 = (ui.w - 2*w0 - w1)/2
local y0 = ui.h - 50
local art1 = [[ui\上一页按钮.tga]]
local art2 = [[ui\下一页按钮.tga]]
ui.page_los = ui:add_button(art1,x0,y0,w0,h0)
ui.page_add = ui:add_button(art2,x0+w0+w1,y0,w0,h0)
ui.page_text = ui:add_text('1/1',x0+w0,y0,w1,h0,10,5)
ui.page = 1
ui.max_page = 1


local button = class.button.create([[ReplaceableTextures\CommandButtons\BTNSelectHeroOn.blp]],1060,10,32,32)
button.hotkey = 'TAB'

function button:on_button_clicked()
    ui:open()
end

ac.game:notify('游戏-注册显示UI',ui,true)


ac.game:event '测试-显示开关'(function(_,name,data)
    if button.show_flag then
        button:hide()
    else
        button:show()
    end
end)

ac.game:event '测试-注册数据'(function(_,name,data,func)
    ui:add_map(name,data)
    if func then
        local ename = '测试-' .. name
        ac.sync:event(ename)(func)
    end
end)
